﻿//zpjs 使用与layui和目前系统中通用一些方法处理，减少代码量和提高一定开发效率
var zpActive = {
    //监听工具条
    tableTool: function (table, layer, form, tableId, tableLayFilter, formSelector, formLayFilter, executeDelFunc, executeFunc1, executeFunc2, executeFunc3, executeFunc4, executeFunc5) {
        tableId = tableId == null ? 'tb_list' : tableId;
        tableLayFilter = tableLayFilter == null ? 'list' : tableLayFilter;
        formSelector = formSelector == null ? '#operateForm' : formSelector;
        formLayFilter = formLayFilter == null ? 'operate' : formLayFilter;
        table.on('tool(' + tableLayFilter + ')', function (obj) {
            zpActive.tableCURD(obj, table, layer, form, tableId, tableLayFilter, formSelector, formLayFilter, executeDelFunc, executeFunc1, executeFunc2, executeFunc3, executeFunc4, executeFunc5);
        });

        //监听事件
        table.on('toolbar(' + tableLayFilter + ')', function (obj) {
            zpActive.tableCURD(obj, table, layer, form, tableId, tableLayFilter, formSelector, formLayFilter, executeDelFunc, executeFunc1, executeFunc2, executeFunc3, executeFunc4, executeFunc5);
        });
    }

    , tableToolBar: function (table, layer, form, tableId, tableLayFilter, formSelector, formLayFilter, executeDelFunc, executeFunc1, executeFunc2, executeFunc3, executeFunc4, executeFunc5) {
        tableLayFilter = tableLayFilter == null ? 'list' : tableLayFilter;
        formSelector = formSelector == null ? '#operateForm' : formSelector;
        formLayFilter = formLayFilter == null ? 'operate' : formLayFilter;

    }

    , tableCURD: function (obj, table, layer, form, tableId, tableLayFilter, formSelector, formLayFilter, executeDelFunc, executeFunc1, executeFunc2, executeFunc3, executeFunc4, executeFunc5) {
        var data = obj.data;

        if (obj.event === 'del') {
            layer.confirm('真的删除行么？', function (index) {
                var arr_obj = new Array();
                arr_obj[0] = obj.data;
                executeDelFunc(arr_obj);   //改造成可以批量删除的，这样可以通用
                //obj.del();
                //layer.close(index);
            });
        } else if (obj.event === 'batchDel') {
            var checkStatus = table.checkStatus(tableId)
                  , selectData = checkStatus.data;
            if (!selectData || selectData == null || selectData.length == 0) {
                dialog.warning('请选择需要操作的数据！');
                return;
            }
            dialog.confirm('真的删除选中行么？', function (index) {
                executeDelFunc(selectData);   //改造成可以批量删除的，这样可以通用
            });
        } else if (obj.event === 'edit' || obj.event === 'add') {
            //layer.alert('zpjs编辑行：<br>' + JSON.stringify(data))
            var formVal = {};
            let itemVal;
            for (let item in data) {
                itemVal = data[item];
                switch (typeof data[item]) {
                    case "string":
                        itemVal = itemVal.indexOf('/Date(') == 0 ? changeDateFormat(itemVal) : itemVal;
                        break;
                    case "number":
                        break;
                    case "boolean":
                        break;
                    case "object":
                        break;
                    case "undefined":
                        break;
                    default:
                }
                eval('formVal.' + eval('item') + '= itemVal;')
            }
            let title = obj.event === 'edit' ? '编辑' : '新增';
            if (obj.event == 'add') {
                $(formSelector).find('[name="id"]').val('0');
                $(formSelector)[0].reset();
            }
            layer.open({
                title: title,
                type: 1,
                area: ['60%', '80%'],
                maxmin: true,
                shadeClose: true,
                //closeBtn: false,  //右上角关闭按钮
                content: $(formSelector),
                shade: 0.5, // 显示遮罩程度0-1
                //btn: ['保存', '关闭'],
                //btnAlign: 'c',
                moveType: 1, //拖拽模式，0或者1
                success: function (layero, index) {
                    executeFunc1 == undefined ? '' : executeFunc1();
                    executeFunc2 == undefined ? '' : executeFunc2();
                    executeFunc3 == undefined ? '' : executeFunc3();
                    executeFunc4 == undefined ? '' : executeFunc4();
                    executeFunc5 == undefined ? '' : executeFunc5();

                    form.val(formLayFilter, formVal);
                    //form.render();  //表单重新渲染，要不然动态数据添加完显示不出来新的
                }
            });
        }
    }
}